var BatchUpdateForm = function () {
  var template = /* html */ `
    <div class="batch-update-form" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto" size="medium">
        <el-form-item required label="维修主管">
          <el-cascader filterable v-model="machineForm.serviceManager" class="update-input" :options="userList" :props="userProps"
            :show-all-levels="false" separator="-"></el-cascader>
        </el-form-item>
        <el-form-item required label="维修技工">
          <el-cascader filterable v-model="machineForm.serviceWorker" class="update-input" :options="userList" :props="userProps"
            :show-all-levels="false" separator="-"></el-cascader>
        </el-form-item>
      </el-form>

      <div style="text-align:center">
        <el-button size="medium" type="primary" @click="ensure">批量修改</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      data: {
        type: Array,
        required: true
      },

      dropdownOption: {
        type: Object,
        defualt: function () {
          return {};
        }
      }
    },

    data: function () {
      return {
        loading: false,
        userList: [],
        machineForm: {
          serviceManager: [],
          serviceWorker: []
        },
        userProps: {
          multiple: true,
          value: 'name',
          label: 'name'
        }
      };
    },

    created: function () {
      this.loading = true;
      var self = this;
      this.setUserList().then(function (userList) {
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      setUserList: function () {
        var dropdownOption = this.dropdownOption;
        var userList = dropdownOption.userList;
        if (userList !== undefined) {
          this.userList = userList;
          return Promise.resolve(userList);
        }

        var self = this;
        return ajaxRequest('workOrder/getCompanyUserTree').then(function (res) {
          var userList = res.rows;

          var userGroup = _.groupBy(userList, function (user) {
            var pid = user.pid;
            if (pid === undefined) {
              return '';
            }
            return pid;
          });

          var users = [];
          for (var key in userGroup) {
            if (key !== '') {
              var obj = {
                name: key
              };
              var children = self.getGroupUsers(userGroup[key]);
              obj.children = children;
              users.push(obj);
            } else {
              var groupUsers = self.getGroupUsers(userGroup[key]);
              users.pushArray(groupUsers);
            }
          }
          users = _.orderBy(users, [function (user) {
            var children = user.children;
            return children ? 0 : 1;
          }], ['asc']);
          self.userList = users;
          dropdownOption.userList = users;
          return users;
        });
      },

      getGroupUsers: function (userList) {
        userList = _.map(userList, function (user) {
          var pid = user.pid;
          return {
            id: user.id,
            name: user.text,
            dept: pid ? pid : null
          };
        });
        return userList;
      },

      ensure: function () {
        var code = this.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`machineRepair.repairMgr.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        var postData = {};
        var machineForm = this.machineForm;
        var serviceManager = machineForm.serviceManager;
        serviceManager = _.map(serviceManager, function (manager) {
          return manager[manager.length - 1];
        });
        postData.serviceManager = serviceManager.join(',');

        var serviceWorker = machineForm.serviceWorker;
        serviceWorker = _.map(serviceWorker, function (worker) {
          return worker[worker.length - 1];
        });
        postData.serviceWorker = serviceWorker.join(',');

        var data = this.data;
        var ids = _.map(data, 'id');
        postData.id = ids.join(',');

        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/updateWorkMachineManagerAndWorksBatch', postData).then(function () {
          self.loading = false;
          self.$alert('设备批量修改成功', '信息', {
            callback: function () {
              self.$emit('update-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      checkValid: function () {
        var machineForm = this.machineForm;
        var fields = ['serviceManager', 'serviceWorker'];
        return CheckVaildUtil.checkNullField(fields, machineForm);
      }
    },

    template: template
  };
}();
